***Note: replicators should set directory to open data 
*cd ""

* FIGURE 6 - 4 subfigures 1,b,c, and d

use "ESS.dta", clear
gen D=D_race
gen PD=PD_race

 label var D "Discriminatory attitudes"
 label var PD "Perceptions of discrimination"

 
 global y1 "D"
 global y2 "PD" 
 global x1 "total_eduyrs"
 global z1 "Age Female Edu_mum  i.Country  i.essround"
 global z2 "Female  Edu_mum i.Country  i.essround"
 global IV "T r"
 global z1b "Age Age2 Female Edu_mum Africa Asia   Muslim   i.Country  i.essround"
 global z2b "Female  Edu_mum   Africa Asia    Muslim  i.Country i.essround"

* FIGURE 6a 
*ETHNORACIAL: discriminatory attitudes

quietly: reg  $y1   $x1  
est store Correlation
local N1=  e(N)
 
quietly:reg  $y1   $x1  $z1  if Monotonic==1, robust
est store OLS
local N2=  e(N)
 
 drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"
 
quietly:rdbwselect  $y1  r if Monotonic==1,  covs(Edu_mum Female r1 r2 r3 r4 r5 r6 r7 r8  c1 c2 c3 c4 c5 c6 c7 c8 c9   c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u1 =floor(e(h_mserd))
g  w=max(0,`u1'-abs(r)) 


quietly: ivreg2  $y1  ($x1 =$IV)    $z2 [pweight= w] if Monotonic==1, first  baselevels robust   gmm2s 
est store IV_all
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y1 if e(sample), mean
loc Meanm:di%8.2fc r(mean)
drop w


quietly: rdbwselect  $y1  r if Strong==1,  covs(Edu_mum Female  r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u2 =floor(e(h_mserd))
g w=max(0,`u2'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = $IV)    $z2  [pweight=w] if Strong==1, first  baselevels robust   gmm2s  
est store IV_restricted
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w

	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color   "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
    grstyle set graphsize 13cm 11.5cm 	
	coefplot  (Correlation, label( "Correlation" "{it:N}=`N1'") ) (OLS, label("OLS" "{it:N}=`N2'") )   (IV_all, label("IV (all)" "{it:N}=`N3'" ))  (IV_restricted, label ("IV (strong)" "{it:N}=`N4'" ) ) ,     keep( total_eduyrs)   xscale(range(-0.2,0.2)) xlab(-0.2(.05)0.2, grid gstyle(minor))   xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))  xtitle("Law against ethnic discrimination are bad", size(medsmall))    ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels     title("{bf:Discriminatory ethnoracial attitudes}")  note("OLS, IV(all) and IV(strong) control for gender,maternal education,country and round effects." "Bandwidths in IV strategies are chosen using  MSE-optimal bandwidth selector." "IV(all) specification: 1st stage {it:F}-stat = `Fm' | bandwidth = `u1' | Outcome mean = `Meanm'." "IV(strong) specification: 1st stage {it:F}-stat = `Fs' | bandwidth = `u2' | Outcome mean = `Means'.", size(vsmall) span)
graph export "Graph/Figure6a.tif", replace 
  
drop total_eduyrs
gen total_eduyrs=  total_eduyrs2
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"


*********************************************************************************************************


* FIGURE 6b

*ETHNORACIAL: perceptions of discrimination
 
la var total_eduyrs "Years of education"


quietly: reg $y2  $x1  
est store Correlation
local N1=  e(N)

quietly:  reg $y2 $x1 $z2b if  Monotonic==1, robust
est store OLS
local N2=  e(N)

drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"

 
quietly:  rdbwselect  $y2  r if Monotonic==1,    covs(Edu_mum Female Africa Asia Muslim     r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9   c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u1 =floor(e(h_mserd))
g w=max(0,`u1'-abs(r)) 



quietly:  ivreg2 $y2  ($x1 = $IV) $z2b  [pweight=w]  if Monotonic==1, first baselevels robust gmm2s 
est store IV_restricted
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meanm=trim("`Mean'")
drop w

quietly:  rdbwselect  $y2  r if Strong==1,    covs(Edu_mum Female Africa Asia Muslim      r1 r2 r3 r4 r5 r6 r7 r8 c1 c2 c3 c4 c5 c6 c7 c8 c9   c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u2 =floor(e(h_mserd))
g w=max(0,`u2'-abs(r)) 

quietly: ivreg2 $y2  ($x1 = $IV)  $z2b  [pweight=w]  if Strong==1, first baselevels robust   gmm2s  
est store IV_all
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w


	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
    grstyle set graphsize 13cm 11.5cm 	
coefplot  (Correlation, label( "Correlation" "{it:N}=`N1'")) (OLS, label("OLS" "{it:N}=`N2'") )   (IV_all, label("IV (all)" "{it:N}=`N3'" )  )  (IV_restricted, label ("IV (strong)" "{it:N}=`N4'" )  ) ,   xscale(range(-0.05,0.05)) xlab(-0.05(.02)0.05, grid gstyle(minor))    keep( total_eduyrs)    xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))  xtitle("Group discriminated against for ethnicity/race", size(medsmall))    ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels     title("{bf:Perceptions of ethnoracial discrimination}")  note("OLS, IV(all) and IV(strong)  control for gender,ethnicity,maternal education,country and round effects." "Bandwidths in IV strategies are chosen using  MSE-optimal bandwidth selector." "IV(all) specification: 1st stage {it:F}-stat = `Fm' | bandwidth = `u1' | Outcome mean = `Meanm'." "IV(strong) specification:  1st stage {it:F}-stat = `Fs' | bandwidth = `u2' | Outcome mean= `Means'.", size(vsmall) span)
	graph export "Graph/Figure6b.tif", replace 
	

 
 
 ***Note: replicators should set directory to open data 
*cd ""

* FIGURE 4_3: basics

use "ESS.dta", clear

gen D=D_gender
gen PD=PD_gender
 
 
 global y1 "D"
 global y2 "PD" 
 global x1 "total_eduyrs"
 global z1 "Age Age2  Edu_mum i.Country  i.essround"
 global z2 "Edu_mum i.Country  i.essround"

 label var D "Discriminatory attitudes"
 label var PD "Perceptions of discrimination"
 
* FIGURE 6c
*GENDER: discriminatory attitudes

quietly: reg  $y1   $x1  
est store Correlation
local N1=  e(N)

quietly: reg  $y1   $x1  $z1   if Monotonic==1, robust
est store OLS
local N2=  e(N)

drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x "total_eduyrs"

quietly: rdbwselect  $y1  r if Monotonic==1,  covs(Edu_mum  r1 r2 r3 r4 r5 r6 r7 r8 c1  c2 c3 c4 c5 c6 c7 c8 c9    c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u1 =floor(e(h_mserd))
g  w=max(0,`u1'-abs(r)) 

quietly: ivreg2  $y1  ($x1 = $IV)   $z2  [pweight=w]  if Monotonic==1,  first baselevels robust   gmm2s 
est store IV_all
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meanm=trim("`Mean'")
drop w

quietly: rdbwselect  $y1  r if Strong==1,  covs(Edu_mum   r1 r2 r3 r4 r5 r6 r7 r8 c1  c2 c3 c4 c5 c6 c7 c8 c9   c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u2 =floor(e(h_mserd)) 
g w=max(0,`u2'-abs(r))


quietly: ivreg2  $y1  ($x1 = $IV)   $z2  [pweight=w] if Strong==1, first baselevels robust   gmm2s 
est store IV_restricted
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y1 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w


	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color     "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
	 grstyle set graphsize 13cm 11.5cm
	coefplot  (Correlation, label( "Correlation" "{it:N}=`N1'")  ) (OLS, label("OLS" "{it:N}=`N2'")  )   (IV_all, label("IV (all)" "{it:N}=`N3'" )  )  (IV_restricted, label ("IV (strong)" "{it:N}=`N4'" ) ) ,     keep(total_eduyrs) ytitle("")  xscale(range(-0.2,0.2)) xlab(-0.2(.05)0.2, grid gstyle(minor))   xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))  xtitle("Men should have priority in labor market", size(medsmall))    ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels     title("{bf:Discriminatory gender attitudes}") note("OLS, IV(all) and IV(strong)  control for maternal education, country and round fixed effects." "Bandwidths in IV strategies are chosen using  MSE-optimal bandwidth selector." "IV(all) specification: 1st stage {it:F}-stat = `Fm' | bandwidth = `u1' | Outcome mean = `Meanm'." "IV(strong) specification: 1st stage {it:F}-stat = `Fs' | bandwidth = `u2' | Outcome mean = `Means'.", size(vsmall) span)
graph export "Graph/Figure6c.tif", replace 

drop total_eduyrs
gen total_eduyrs=  total_eduyrs2
la var total_eduyrs "Years of education"
global x1 "total_eduyrs"

************************************************************************************************************

* FIGURE 6d
*GENDER: perceptions of attitudes
 
la var total_eduyrs "Years of education"

quietly: reg  $y2   $x1   
est store Correlation
local N1= e(N)

quietly: reg  $y2   $x1  $z1    if Monotonic==1, robust
est store OLS
local N2= e(N)

drop total_eduyrs
gen total_eduyrs=eduyrs
la var total_eduyrs "Years of education"
global x "total_eduyrs"

quietly: rdbwselect  $y2  r if Monotonic==1,    covs(Edu_mum  r1 r2 r3 r4 r5 r6 r7 r8    c1  c2 c3 c4 c5 c6 c7 c8 c9  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u1 =floor(e(h_mserd))
g  w=max(0,`u1'-abs(r)) 

quietly: ivreg2  $y2  ($x1 = $IV)   $z2 [pweight=w]  if Monotonic==1,  first baselevels robust   gmm2s 
est store IV_all
mat l e(first)
local Fm= floor(e(first)[rownumb(e(first),"SWF"),1])
local N3=  e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Meanm=trim("`Mean'")

drop w

quietly: rdbwselect  $y2  r if Strong==1,  covs(Edu_mum  r1 r2 r3 r4 r5 r6 r7 r8 c1  c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 )
local u2 =floor(e(h_mserd))
g w=max(0,`u2'-abs(r))


quietly: ivreg2  $y2  ($x1 = $IV)   $z2  [pweight=w] if Strong==1, first baselevels robust   gmm2s 
est store IV_restricted
mat l e(first)
local Fs= floor(e(first)[rownumb(e(first),"SWF"),1])
local N4= e(N)
su  $y2 if e(sample), mean
loc Mean:di%8.2fc r(mean)
loc Means=trim("`Mean'")
drop w


	   grstyle init
	 grstyle set legend 2,  nobox
	 grstyle set size 8pt: tick_label key_label
	 grstyle set size 12pt: heading
	 grstyle set size 10pt: subheading axis_title
	grstyle set color     "180 180 180" "130 130 130" "65 65 65"  "0 0 0" 
	    grstyle set graphsize 13cm 11.5cm 
coefplot  (Correlation, label( "Correlation" "{it:N}=`N1'")) (OLS, label("OLS" "{it:N}=`N2'") )   (IV_all, label("IV (all)" "{it:N}=`N3'" )  )  (IV_restricted, label ("IV (strong)" "{it:N}=`N4'" )  ) ,  xscale(range(-0.05,0.05)) xlab(-0.05(.02)0.05, grid gstyle(minor))   keep( total_eduyrs)    xline(0,  lpattern(dash)  )   msymbol(d)  levels(95) ciopts(recast(. rcap))  xtitle("Group discriminated against for gender", size(medsmall))     ylabel(,angle(vertical) labsize(medsmall))  legend(position(12) rows(1) span )  baselevels    title("{bf:Perceptions of gender discrimination}")  note("OLS, IV and IV(strong)  control for maternal education, country and round fixed effects." "Bandwidths in IV strategies are chosen using  MSE-optimal bandwidth selector." "IV(all) specification: 1st stage {it:F}-stat = `Fm' | bandwidth = `u1' | Outcome mean =`Meanm'." "IV(strong) specification:  1st stage {it:F}-stat = `Fs' | bandwidth = `u2' | Outcome mean = `Means'.", size(vsmall) span)       
graph export "Graph/Figure6d.tif", replace 
 